﻿using Pay.Data;
using Pay.DTO;
using Pay.IService;
using Pay.Service.Cache;
using Pay.TO;
using Pay.Core;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Pay.Service.Service {
    public class DistributionProductService : ServiceBase, IDistributionProductService, IDisposable {
        private IUnitOfWork _iUnitOfWork;

        public DistributionProductService(IUnitOfWork iUnitOfWork) : base(iUnitOfWork) {
            _iUnitOfWork = iUnitOfWork;
        }

        public int Add(distribution_product_dto dto) {
            var product = context.Products.FirstOrDefault(c => c.Id == dto.productid);
            if (product.MerchantId == dto.merchantid)
                throw new ServerErrorException("自己不能分销自己商品");
            if (product.SaleStatus != SaleStatus.Sell)
                throw new ServerErrorException("该商品不是已销售状态");
            var shopInfo = context.ShopInfo.FirstOrDefault(c => c.Id == dto.merchantid);
            if (shopInfo.IsDisabled)
                throw new ServerErrorException("该店铺已被禁用");
            if (context.DistributionProducts.Where(c => c.MerchantId == dto.merchantid && c.Status != EDistributionProductStatus.Deleted).Count() > 30)
                throw new ServerErrorException("分销商品不可以超过30款");

            var model = dto.ToDtoObject<DistributionProduct>();
            bool has_share = context.DistributionProducts.Any(c => c.ProductId == dto.productid && c.MerchantId == dto.merchantid);
            if (has_share) {
                var disProduct = context.DistributionProducts.FirstOrDefault(c => c.ProductId == dto.productid && c.MerchantId == dto.merchantid);
                disProduct.Status = EDistributionProductStatus.InSales;
            } else {
                model.CategoryId = 0;
                model.Status = EDistributionProductStatus.InSales;
                context.DistributionProducts.Add(model);
            }
            return _iUnitOfWork.Commit("DistributionProduct" + dto.merchantid);
        }

        public List<distribution_product_dto> Get(long Id) {
            var result = context.DistributionProducts.Where(x => x.MerchantId == Id).FromCache<DistributionProduct, distribution_product_dto>();
            return result;
        }

        public int SoldOut(long id) {
            var model = context.DistributionProducts.Find(id);
            context.DistributionProducts.Remove(model);
            int result = _iUnitOfWork.Commit("DistributionProduct" + model.MerchantId);
            return result;
        }

        /// <summary>
        /// 注意，只有当用户查询时做缓存
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public PagedResult<product_dto> GetProducts(GoodsQuery query) {
            var products = context.DistributionProducts.Where(c => c.MerchantId == query.sell_id).Select(c => c.Product).FromCache<Product, product_dto>("DistributionProduct" + query.sell_id);
            return new PagedResult<product_dto>(products, products.Count(), query.page, query.size);
        }
    }
}
